<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\services\impl;


use ckeeper\dao\RoleDao;
use ckeeper\entity\RoleEntity;
use ckeeper\services\RoleService;
use Selibra\Di\Annotations\Autowired;
use Selibra\Model\Exception\DbException;
use Selibra\Tools\Console;

class RoleServiceImpl implements RoleService
{

    /**
     * @var RoleDao
     */
    #[Autowired]
    protected RoleDao $roleDao;

    /**
     * @param int[] $ids
     * @return string[]
     */
    public function getRightsByIds(array $ids): array
    {
        Console::log($this->roleDao);
        $roles = $this->roleDao->selectRolesByIds($ids);
        if (empty($roles)) {
            return [];
        }
        $rights = [];
        foreach ($roles as $role) {
            $roleRights = $role->getRights();
            if (!empty($roleRights)) {
                $roleRights = explode(',', $roleRights);
                $rights = array_merge($rights, $roleRights);
            }
        }
        return array_values(array_unique($rights));
    }

    /**
     * 添加角色
     * @param RoleEntity $roleEntity
     * @return int|bool
     */
    public function addRole(RoleEntity $roleEntity): int|bool
    {
        if (empty($roleEntity->getName())) {
            return false;
        }
        try {
            $id = $this->roleDao->addRole($roleEntity);
        } catch (DbException $dbException) {
            return false;
        }
        return $id;
    }

    /**
     * 获取所有角色
     * @param string|null $keyword 关键字搜索
     * @return array
     */
    public function searchRole(string $keyword = null): array
    {
        if (!empty($keyword)) {
            $this->roleDao->getCondition()
                ->orWhere('name', 'like', "%{$keyword}%")
                ->orWhere('description', 'like', "%{$keyword}%");
        }
        $this->roleDao->getSqlBuilder()->setSort('`id` desc');
        return $this->roleDao->select();
    }


    /**
     * 修改角色数据
     * @param int $id
     * @param string $key
     * @param string $value
     * @return bool
     */
    public function updateRoleData(int $id, string $key, string $value): bool
    {
        try {
            $this->roleDao->getCondition()->andWhere('id', '=', $id);
            $this->roleDao->updateDataByField($id, $key, $value);
        } catch (DbException $dbException) {
            return false;
        }
        return true;
    }

    /**
     * 修改角色数据
     * @param RoleEntity $roleEntity
     * @return mixed
     */
    public function updateRole(RoleEntity $roleEntity)
    {
        try {
            $this->roleDao->updateData($roleEntity->getId(), $roleEntity);
        } catch (DbException $dbException) {
            return false;
        }
        return true;
    }

    /**
     * 通过ID获取角色
     * @param int $id
     * @return RoleEntity
     */
    public function getRoleById(int $id)
    {
        $this->roleDao->getCondition()->andWhere('id', '=', $id);
        return $this->roleDao->get();
    }


    /**
     * 通过Id列表删除角色
     * @param array $ids
     * @return mixed
     */
    public function deleteRoleByIds(array $ids)
    {
        $key = array_search(1,$ids);
        // 不允许删除超级管理员
        if( !empty($key) ){
            unset($ids[$key]);
        }
        if( empty($ids) ){
            return true;
        }
        try{
            $this->roleDao->getCondition()->andWhere('id', 'in', implode(',', $ids));
             $this->roleDao->delete();
        }catch (DbException $dbException){
            return false;
        }
        return true;
    }
}
